/**
 * Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations
 * under the License.
 */
package cn.well.cloud.exception.aop;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import cn.well.cloud.common.HttpHeader;
import cn.well.cloud.exception.enums.BizExceptionEnum;

import com.alibaba.fastjson.JSONObject;

/**
 * 请求体检测 AOP Created by wison on 2019年8月20日.
 */
@Aspect
@Component
@Order(200)
public class RequestAop {

    private final Log logger = LogFactory.getLog(this.getClass());
    
    
    @Autowired
    protected HttpServletRequest request;

    @Pointcut(value = "@annotation(cn.well.cloud.exception.annotion.RequestBodyCheck)")
    private void cutRequestBody() {

    }

    @Around("cutRequestBody()")
    public Object doUserIdentity(ProceedingJoinPoint point) throws Throwable {
        Object result = null;
        checkRequestBody(point);
        result = point.proceed();
        return result;
    }

    private void checkRequestBody(ProceedingJoinPoint point) {
        
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
        } catch (IOException e) { 
            e.printStackTrace();
            throw new ServiceException(BizExceptionEnum.REQUEST_METHOD_ERROR);
        }
        String line = null;
        StringBuilder sb = new StringBuilder();
        try {
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new ServiceException(BizExceptionEnum.REQUEST_METHOD_ERROR);
        }
        HttpHeader httpHeader = HttpHeader.local.get();
        httpHeader.setRequestBody(JSONObject.parseObject(sb.toString()));
        HttpHeader.set(httpHeader);
    }

}
